package com.application.lucky.presenter;

import android.text.TextUtils;

import com.application.lucky.bean.jsonBean.LoginBean;
import com.application.lucky.manager.UserManagers;
import com.application.lucky.request.LoginRequest;
import com.application.lucky.view.RegisterView;
import com.application.mylibrary.base.mvp.IBasePresenter;
import com.application.mylibrary.utils.LogUtils;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;
import com.baselib.libnetwork.net.libnetworkcomponent.RequestCommonParams;

import java.util.Random;

public class RegisterPresenter extends IBasePresenter<RegisterView> {

    private final LoginRequest request;
    private int numCode;

    public RegisterPresenter() {
        request = new LoginRequest();

    }


    public void register() {

        if (getView() != null) {
            String num = getView().getNum();
            if ((numCode + "").equals(num)) {
                final String username = getView().getUsername();
                if (TextUtils.isEmpty(username)) {

                    getView().showToast("用户名不能为空");
                    return;

                }
                if (username.length() > 15 || username.length() < 6) {
                    getView().showToast("用户名为6-15位，请重新输入");
                    return;
                }

                String password = getView().getPassword();
                String password2 = getView().getPassword2();

                if (password.length() > 12 || password.length() < 6 || password2.length() > 12 || password2.length() < 6) {
                    getView().showToast("密码为6-12位，请重新输入");
                    return;
                }

                if (password.equals(password2)) {

                    request.register(username, password, new LoginRequest.LoginBack<BaseResponse<LoginBean>>() {

                        @Override
                        public void success(BaseResponse<LoginBean> s) {
                            LogUtils.i("lucky_log  token===" + s);
                            LoginBean dataInfo = s.dataInfo;
                            if (getView() != null) {
                                String s1 = getView().getetINviteCode();
                                boolean empty = TextUtils.isEmpty(s1);
                                if (!empty) {
                                    UserManagers.getInstance().invite(s1);
                                }
                            }
                            if (dataInfo != null) {
                                String token = dataInfo.token;
                                RequestCommonParams.getInstance().updateToken(token);
                                UserManagers.getInstance().saveToken(token);
                                UserManagers.getInstance().saveLoginName(username);
                                UserManagers.getInstance().loadUserBean();

                            }

                            if (getView() != null) {
                                getView().Success();
                            }

                        }

                        @Override
                        public void fail(int code) {
                            if (getView() != null) {
                                getView().error(code);
                            }
                        }
                    });
                } else {
                    getView().showToast("两次密码不一致，请重新输入");
                }

            } else {
                getView().showToast("验证码不正确，请重新输入");

            }


        }
    }


    public void setNUm() {
        numCode = (int) (Math.random() * (9999 - 1000 + 1)) + 1000;
        if (getView() != null) {
            getView().setNumcode(numCode + "");
        }
    }
}
